package com.itzdm.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.itzdm.mapper.UserMapper;
import com.itzdm.pojo.Car;
import com.itzdm.pojo.Result;
import com.itzdm.pojo.User;
import com.itzdm.service.CarService;
import com.itzdm.service.UserService;
import com.itzdm.utils.JwtUtils;
import com.itzdm.utils.TimeUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CarService carService;

    @Autowired
    private TimeUtils timeUtils;

    @Override
    public Result rent(String token,JSONObject jsonObject) {
        Claims claims = JwtUtils.parseJWT(token);
        String username = (String) claims.get("username");
        Integer vehicleId = jsonObject.getInteger("vehicleId");
        Integer days = jsonObject.getInteger("days");
        Integer userId = userMapper.getUserId(username);
        Double payMoney = carService.payMoney(userId,vehicleId,days);
        String beginTime = timeUtils.getCurrentTime();
        String deadline = timeUtils.getDeadline(days);
        carService.addVehicleStatus(userId,beginTime,deadline);
        Integer vehicleStatusId = carService.getStatusId(userId,beginTime,deadline);
        carService.updateVehicleStatus(vehicleId,vehicleStatusId);
        return Result.success(payMoney,"租车成功");
    }

    @Override
    public void backCar(Car car) {
        //userMapper.backCar(car);
    }

    @Override
    public Result login(User user) {
        String username = user.getUsername();
        int count = userMapper.isExit(username);
        if (count == 0) {
            return Result.error("用户名不存在");
        }
        String pwd = user.getPwd();
        if (!pwd.equals(userMapper.getPwd(username))) {
            return Result.error("密码输入错误");
        }
        Map<String, Object> redis = new HashMap<>();
        redis.put("username", username);
        String jwt = JwtUtils.generateJwt(redis);
        return Result.success(jwt, "登陆成功");
    }

    @Override
    public Result register(String username,String pwd,String name,String idNumber) {
        if (idNumber.length() != 18) return Result.error("身份证长度不对");
        if (userMapper.isExit(username) != 0) return Result.error("该用户名已经存在");
        if (userMapper.IdisExit(idNumber) != 0) return Result.error("身份证已存在");
        userMapper.addUser(username,pwd,name,idNumber);
        return Result.success("注册成功");
    }

    @Override
    public Result charge(String token,JSONObject jsonObject) {
        double chargeMoney = jsonObject.getDouble("chargeMoney");
        Claims claims = JwtUtils.parseJWT(token);
        String username = (String) claims.get("username");
        double money = userMapper.getMoney(username);
        money += chargeMoney;
        userMapper.charge(money,username);
        return Result.success("充值成功");
    }

    @Override
    public double getInfo(String token) {
        Claims claims = JwtUtils.parseJWT(token);
        String username = (String) claims.get("username");
        return userMapper.getMoney(username);
    }
}
